package org.mch.func.sys.func;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import org.apache.commons.beanutils.BeanUtils;
import org.mch.biz.ui.BizEvent;
import org.mch.biz.ui.BizPanel;
import org.mch.comm.utils.MFormUITools;
import org.mch.func.entity.MFuncEO;
import org.mch.swingm.core.MButton;
import org.mch.swingm.core.MTree;
import org.mch.swingm.ext.METextField;
import org.mch.swingm.itf.IMExtComponent;
import org.mch.swingm.obj.MItem;

public class SysFuncPanel extends BizPanel {
	private static final long serialVersionUID = 1L;
	private JSplitPane splitPane;
	private JPanel panel;
	private JPanel panel_right;
	private JScrollPane scrollPane;
	private MTree tree;
	private SysFuncPanelModel panelModel;

	protected void initMCH() {
		super.initMCH();
		setLayout(new BorderLayout(0, 0));
		add(getSplitPane(), "Center");
		add(getPanel_button(), BorderLayout.NORTH);
		buildFormUI();
		buildButtons();
	}

	public JSplitPane getSplitPane() {
		if (this.splitPane == null) {
			this.splitPane = new JSplitPane();
			splitPane.setDividerSize(5);
			this.splitPane.setLeftComponent(getPanel());
			this.splitPane.setRightComponent(getPanel_right());
			this.splitPane.setDividerLocation(200);
		}
		return this.splitPane;
	}

	public JPanel getPanel() {
		if (this.panel == null) {
			this.panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getScrollPane(), BorderLayout.CENTER);
		}
		return this.panel;
	}

	public JPanel getPanel_right() {
		if (this.panel_right == null) {
			this.panel_right = new JPanel();
			panel_right.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		}
		return this.panel_right;
	}

	protected BizEvent createEvent() {
		return new SysFuncPanelEvent(this);
	}

	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTree());
		}
		return scrollPane;
	}

	public class FunTreeClickEvent extends MouseAdapter {
		@Override
		public void mousePressed(MouseEvent e) {
			try {
				onClickTree(e);
			} catch (Exception e1) {
				log().error(e1);
			}
		}
	}

	/**
	 * 双击树事件方法；
	 * 
	 * @param e
	 * @throws Exception
	 */
	protected void onClickTree(MouseEvent e) throws Exception {
		JTree tree = (JTree) e.getSource();
		int selrow = tree.getRowForLocation(e.getX(), e.getY());
		TreePath selpath = tree.getPathForLocation(e.getX(), e.getY());
		if (selrow == -1) {
			return;
		}

		/*
		 * 如果双击的是树功能节点，则构建显示对应功能Panel；
		 */
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) selpath.getLastPathComponent();
		MFuncEO nodeEO = (MFuncEO) node.getUserObject();

		String[] keys = itemComp.keySet().toArray(new String[0]);
		for (String k : keys) {
			IMExtComponent comp = itemComp.get(k);
			((METextField) comp).setValue(BeanUtils.getProperty(nodeEO, k));
		}
	}

	private FunTreeClickEvent treeEvent;

	public FunTreeClickEvent getTreeEvent() {
		if (treeEvent == null) {
			treeEvent = new FunTreeClickEvent();
		}
		return treeEvent;
	}

	public MTree getTree() {
		if (tree == null) {
			tree = new MTree();
			tree.setModel(new SysFuncTreeModel().getTreeModel());
			tree.addMouseListener(getTreeEvent());
		}
		return tree;
	}

	private JPanel panel_button;

	protected void buildFormUI() {
		List<MItem> items = getPanelModel().getFieldItems();
		for (MItem item : items) {
			IMExtComponent component = MFormUITools.buildComponent(item);
			itemComp.put(item.getKey(), component);
			getPanel_right().add((JComponent) component);
		}
	}

	public void setItemValue(String key, Object value) {
		itemComp.get(key).setValue(value);
	}

	public Object getItemValue(String key) {
		return itemComp.get(key).getValue();
	}

	public IMExtComponent getItemComp(String key) {
		return itemComp.get(key);
	}

	public MFuncEO getFormUIData() {
		MFuncEO eo = new MFuncEO();

		List<IMExtComponent> items = new ArrayList<IMExtComponent>(itemComp.values());
		for (IMExtComponent comp : items) {
			try {
				BeanUtils.setProperty(eo, comp.getKey(), comp.getValue());
			} catch (Exception e) {
				log().error(e);
			}
		}

		return eo;
	}

	protected void buildButtons() {
		List<MButton> items = getPanelModel().getButtons();
		for (MButton btn : items) {
			btn.addMouseListener(getEvent());
			getPanel_button().add(btn);
		}
	}

	public JPanel getPanel_button() {
		if (panel_button == null) {
			panel_button = new JPanel();
			FlowLayout fl_panel_button = (FlowLayout) panel_button.getLayout();
			fl_panel_button.setAlignment(FlowLayout.LEFT);
		}
		return panel_button;
	}

	public SysFuncPanelModel getPanelModel() {
		if (panelModel == null) {
			panelModel = new SysFuncPanelModel();
		}
		return panelModel;
	}
}